﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ExtensionsAPI.ContactsAPI;

namespace Core.Basic.ContactAPI {
	public class Email : IEmail {
		public Email(string email = null) {
			if (email !=null && Valid(email)) {
				string[ ] parts = email.Split('@');
				this.Login = parts[0];
				this.Server = parts[1];
			}
		}

		public string Login { get; set; }
		public string Server { get; set; }

		public override string ToString( ) {
			return this.Login + "@" + this.Server;
		}

		public static bool Valid(string email) {
			//^[a-z0-9]+([\.\-_][a-z0-9]+)*@([a-z0-9]+([\.\-_][a-z0-9]+)*\.([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi)|([0-9]{1,3}(\.[0-9]{1,3}){3})|([[0-9a-f]{1,4}(:[0-9a-f]{0,4}){2,7}]))(:[0-9]{1,5})?$
			string regex = @"^[a-z0-9]+([\.\-_][a-z0-9]+)*@";
			regex += @"([a-z0-9]+([\.\-_][a-z0-9]+)*\.([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi)";//domain
			regex += @"|([0-9]{1,3}(\.[0-9]{1,3}){3})";//IPv4
			regex += @"|(\[[0-9a-f]{1,4}(:[0-9a-f]{0,4}){2,7}\]))";//IPv6
			regex += @"(:[0-9]{1,5})?$";//Port
			Regex pattern = new Regex(regex);
			return pattern.IsMatch(email);
		}
	}
}
